﻿using DBToExcel.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBToExcel.Code
{
    public class FileConfig
    {
        private static string _DriverDir = Path.Combine(Environment.CurrentDirectory, "driver");
        private static string _ExcelTemplateDir = Path.Combine(Environment.CurrentDirectory, "template");

        /// <summary>
        /// 获取所有连接
        /// </summary>
        /// <returns></returns>
        public static List<ComboBoxItem> GetDrivers()
        {
            if (!Directory.Exists(_DriverDir))
            {
                Directory.CreateDirectory(_DriverDir);
            }
          

            DirectoryInfo directoryInfo = new DirectoryInfo(_DriverDir);
            FileInfo[] drs = directoryInfo.GetFiles("*.dll");
            if (drs == null || drs.Length == 0)
            {
                return null;
            }

            List<ComboBoxItem> list = new List<ComboBoxItem>();
            foreach (FileInfo item in drs)
            {
                list.Add(new ComboBoxItem()
                {
                    Text = item.Name,
                    Value = item.FullName
                });
            }

            return list;
        }

        /// <summary>
        /// 保存Excel模板
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string SaveExcelTempLate(Stream stream)
        {
            if (!Directory.Exists(_ExcelTemplateDir))
            {
                Directory.CreateDirectory(_ExcelTemplateDir);
            }

            string filename = null;
            for (int i = 0; i < 10; i++)
            {
                string tempFileName = Path.Combine(_ExcelTemplateDir, Guid.NewGuid().ToString() + ".tmp");
                if (!File.Exists(tempFileName))
                {
                    filename = tempFileName;
                    break;
                }
            }

            if (string.IsNullOrEmpty(filename))
            {
                return null;
            }

            FileStream fileStream= File.Open(filename, FileMode.OpenOrCreate);
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            fileStream.Write(buffer, 0, buffer.Length);
            fileStream.Flush();
            fileStream.Close();
            return filename;
        }


        /// <summary>
        /// 清理模板
        /// </summary>
        public static void CLearTmp()
        {
            List<MTemplate> temps = ExcelBuilderService.QueryTempLates(null);
            if (temps == null || temps.Count == 0)
            {
                return;
            }


            if (!Directory.Exists(_ExcelTemplateDir))
            {
                return;
            }

          string[] files=  Directory.GetFiles(_ExcelTemplateDir);
            if (files==null||files.Length==0)
            {
                return;
            }

            List<string> usedTemps = temps.FindAll(x => x.IsTempLate == 1 && !string.IsNullOrEmpty(x.TempPlateExcelFile)).Select(x => x.TempPlateExcelFile).ToList();
            List<string> delFiles = files.ToList().FindAll(x => usedTemps.IndexOf(x) == -1).ToList();
            foreach (var item in delFiles)
            {
                try
                {
                    File.Delete(item);
                }
                catch (Exception)
                {

                }
            }
        }
    }
}
